<template>
  <div class="edit-box">
    <el-form
      :model="form"
      label-position="left"
      label-width="120px"
      :rules="formRules"
      ref="ruleForm"
    >
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="form.projectName"></el-input>
      </el-form-item>
      <el-form-item label="类别" prop="allTypeId">
        <el-cascader
          v-model="form.allTypeId"
          :show-all-levels="false"
          :options="typeOptions"
          :props="{value: 'id',label: 'name', checkStrictly: true, emitPath: false}"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="项目负责人" prop="leadingId">
        <el-select
          v-model="form.leadingId"
          filterable
          remote
          reserve-keyword
          placeholder="请输入关键词"
          :remote-method="remoteUserList"
          :loading="loading"
        >
          <el-option
            v-for="item in userOptions"
            :key="item.id"
            :label="item.userName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="时间范围">
        <el-date-picker
          v-model="date"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" :rows="2" v-model="form.remark"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('changeState',false)">取 消</el-button>
      <el-button type="primary" @click="submit">保存</el-button>
    </div>
  </div>
</template>
<script>
import { update, findById } from '@/api/project'
import { getListByName } from '@/api/user'
import { findList } from '@/api/type'
export default {
  data() {
    return {
      loading: false,
      date: [],
      form: {
        id: '',
        date: '',
        companyId: this.$store.state.user.companyId,
        leadingName: '',
        projectName: '',
        beginTime: '',
        endTime: '',
        allTypeId: '',
        remark: ''
      },
      typeOptions: [],
      userOptions: [],
      formRules: {
        projectName: [
          { required: true, message: '请输入项目名称', trigger: 'blur' }
        ],
        leadingId: [
          { required: true, message: '请输入服务人', trigger: 'blur' }
        ]
      }
    }
  },
  beforeDestroy() {
    this.$off('reload')
    this.$off('changeState')
  },
  mounted() {
    this.remoteUserList('')
    this.initTypeOptions()
  },
  methods: {
    initDialogData(id) {
      findById(id).then(res => {
        this.form = res.data
        this.date.push(res.data.beginTime)
        this.date.push(res.data.endTime)
      })
    },
    initTypeOptions() {
      findList(this.form.companyId, 0).then(res => {
        this.typeOptions = res.data
      })
    },
    // 获取用户列表
    remoteUserList(query) {
      this.loading = true
      let params = { companyId: this.form.companyId, userName: query }
      getListByName(params).then(res => {
        this.loading = false
        this.userOptions = res.data
      })
    },
    submit() {
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          this.form.beginTime = this.date[0]
          this.form.endTime = this.date[1]
          update(this.form).then(res => {
            if (res.success) {
              this.$emit('reload')
              this.$emit('changeState', false)
              this.$message.success(res.message)
            } else {
              this.$message.error(res.message)
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.edit-box {
  padding: 0 50px;
  overflow-y: auto;
}
.dialog-footer {
  text-align: right;
}
</style>
